package com.slipper.system.schedule.service;

import java.util.Date;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.system.schedule.dao.ScheduleLogDao;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import com.slipper.system.schedule.bo.ScheduleLogBO;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.DateUtil;

@Service
@ReadTransactional
public class ScheduleLogService {
    @Resource
    private ScheduleLogDao scheduleLogDao;
    
    public ScheduleLogBO queryBy(ScheduleLogBO bo) {
        return scheduleLogDao.queryBy(bo.getId());
    }
    
    public PageInfo<ScheduleLogBO> queryPage(ScheduleLogBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(scheduleLogDao.queryList(bo));
    }
    
    @WriteTransactional
    public ScheduleLogBO save(ScheduleLogBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            scheduleLogDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setCreateTime(System.currentTimeMillis());
            scheduleLogDao.insert(bo);
        }
        
        return bo;
    }
    
    @WriteTransactional
    public Integer delete(ScheduleLogBO bo) {
        return scheduleLogDao.delete(bo);
    }

    public Boolean isRunning(ScheduleLogBO bo) {
        bo.setStatus(ScheduleLogBO.STATUS_RUNNING);
        bo.setStartDate(DateUtil.getNextHour(new Date(), -12));
        return !this.scheduleLogDao.queryList(bo).isEmpty();
    }
}
